<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title> - 古一 - 个人博客</title>
  <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="古一" /><meta name="description" content="一. 案例代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64" /><meta name="keywords" content="古一, 博客, 网站" />






<meta name="generator" content="Hugo 0.81.0 with theme even" />


<link rel="canonical" href="/post/mongodb/mongodb%E4%BC%98%E5%8C%96/" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">



<link href="/sass/main.min.af7fd1da18d66c2b017df5b4cae508ef44cfcac3fb4c7c7a327fe4f4f9e28b08.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.css" integrity="sha256-7TyXnr2YU040zfSP+rEcz29ggW4j56/ujTPwjMzyqFY=" crossorigin="anonymous">


<meta property="og:title" content="" />
<meta property="og:description" content="一. 案例代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64" />
<meta property="og:type" content="article" />
<meta property="og:url" content="/post/mongodb/mongodb%E4%BC%98%E5%8C%96/" /><meta property="article:section" content="post" />



<meta itemprop="name" content="">
<meta itemprop="description" content="一. 案例代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64">

<meta itemprop="wordCount" content="2211">
<meta itemprop="keywords" content="" /><meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content=""/>
<meta name="twitter:description" content="一. 案例代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64"/>

<!--[if lte IE 9]>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->

</head>
<body>
  <div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/" class="logo">古一</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    <a href="/">
        <li class="mobile-menu-item">首页</li>
      </a><a href="/post/">
        <li class="mobile-menu-item">文章</li>
      </a><a href="/tags/">
        <li class="mobile-menu-item">标签</li>
      </a><a href="/categories/">
        <li class="mobile-menu-item">分类</li>
      </a><a href="/about/">
        <li class="mobile-menu-item">关于</li>
      </a>
  </ul>

  


</nav>

  <div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/" class="logo">古一</a>
</div>





<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
        <a class="menu-item-link" href="/">首页</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/post/">文章</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/tags/">标签</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/categories/">分类</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/about/">关于</a>
      </li>
  </ul>
</nav>

    </header>

    <main id="main" class="main">
      <div class="content-wrapper">
        <div id="content" class="content">
          <article class="post">
    
    <header class="post-header">
      <h1 class="post-title"></h1>

      <div class="post-meta">
        <span class="post-time"> 0001-01-01 </span>
        
          <span class="more-meta"> 约 2211 字 </span>
          <span class="more-meta"> 预计阅读 5 分钟 </span>
        
      </div>
    </header>

    <div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">文章目录</h2>
  <div class="post-toc-content always-active">
    <nav id="TableOfContents">
  <ul>
    <li>
      <ul>
        <li><a href="#一-案例代码">一. 案例代码</a>
          <ul>
            <li><a href="#1-注意">1. 注意</a></li>
            <li><a href="#execstats层stage分析">execStats层，Stage分析</a></li>
          </ul>
        </li>
      </ul>
    </li>
  </ul>
</nav>
  </div>
</div>
    <div class="post-content">
      <h2 id="一-案例代码">一. 案例代码</h2>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span><span class="lnt">42
</span><span class="lnt">43
</span><span class="lnt">44
</span><span class="lnt">45
</span><span class="lnt">46
</span><span class="lnt">47
</span><span class="lnt">48
</span><span class="lnt">49
</span><span class="lnt">50
</span><span class="lnt">51
</span><span class="lnt">52
</span><span class="lnt">53
</span><span class="lnt">54
</span><span class="lnt">55
</span><span class="lnt">56
</span><span class="lnt">57
</span><span class="lnt">58
</span><span class="lnt">59
</span><span class="lnt">60
</span><span class="lnt">61
</span><span class="lnt">62
</span><span class="lnt">63
</span><span class="lnt">64
</span><span class="lnt">65
</span><span class="lnt">66
</span><span class="lnt">67
</span><span class="lnt">68
</span><span class="lnt">69
</span><span class="lnt">70
</span><span class="lnt">71
</span><span class="lnt">72
</span><span class="lnt">73
</span><span class="lnt">74
</span><span class="lnt">75
</span><span class="lnt">76
</span><span class="lnt">77
</span><span class="lnt">78
</span><span class="lnt">79
</span><span class="lnt">80
</span><span class="lnt">81
</span><span class="lnt">82
</span><span class="lnt">83
</span><span class="lnt">84
</span><span class="lnt">85
</span><span class="lnt">86
</span><span class="lnt">87
</span><span class="lnt">88
</span><span class="lnt">89
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-json" data-lang="json"><span class="p">{</span>
    <span class="nt">&#34;op&#34;</span><span class="p">:</span> <span class="s2">&#34;query&#34;</span><span class="p">,</span> <span class="err">#</span> <span class="err">操作类型，值可为command、count、distinct、geoNear、getMore、group、insert、mapReduce、query、remove、update</span>
    <span class="nt">&#34;ns&#34;</span><span class="p">:</span> <span class="s2">&#34;test.exam_note&#34;</span><span class="p">,</span> <span class="err">#</span> <span class="err">操作的数据库和集合</span>
    <span class="nt">&#34;query&#34;</span><span class="p">:</span> <span class="p">{</span> 
        <span class="nt">&#34;find&#34;</span><span class="p">:</span> <span class="s2">&#34;exam_note&#34;</span><span class="p">,</span>  <span class="err">#</span> <span class="err">操作的集合</span>
        <span class="nt">&#34;filter&#34;</span><span class="p">:</span> <span class="p">{</span> <span class="err">#</span> <span class="err">查询条件</span>
            <span class="nt">&#34;student_id&#34;</span><span class="p">:</span> <span class="s2">&#34;5c90d9162fb1c568db4b574b&#34;</span><span class="p">,</span>
            <span class="nt">&#34;km&#34;</span><span class="p">:</span> <span class="s2">&#34;km2&#34;</span>
        <span class="p">},</span>
        <span class="nt">&#34;limit&#34;</span><span class="p">:</span> <span class="p">{</span>
            <span class="nt">&#34;$numberLong&#34;</span><span class="p">:</span> <span class="s2">&#34;1&#34;</span>
        <span class="p">}</span>
    <span class="p">},</span>
    <span class="nt">&#34;keysExamined&#34;</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span> <span class="err">#</span> <span class="err">MongoDB为执行操作而扫描的索引键的数量</span>
    <span class="nt">&#34;docsExamined&#34;</span><span class="p">:</span> <span class="mi">63829</span><span class="p">,</span> <span class="err">#</span> <span class="err">MongoDB为了执行操作而扫描的集合中的文档数</span>
    <span class="nt">&#34;cursorExhausted&#34;</span><span class="p">:</span> <span class="kc">true</span><span class="p">,</span>
    <span class="nt">&#34;numYield&#34;</span><span class="p">:</span> <span class="mi">499</span><span class="p">,</span> <span class="err">#</span> <span class="err">让步次数，操作时让其他的操作完成的次数。</span>
    <span class="nt">&#34;locks&#34;</span><span class="p">:</span> <span class="p">{</span> <span class="err">#</span> <span class="err">操作期间的锁和所的类型</span>
        <span class="nt">&#34;Global&#34;</span><span class="p">:</span> <span class="p">{</span> <span class="err">#</span> <span class="err">表示全局锁定</span>
            <span class="nt">&#34;acquireCount&#34;</span><span class="p">:</span> <span class="p">{</span> <span class="err">#</span> <span class="err">锁定的次数</span>
                <span class="nt">&#34;r&#34;</span><span class="p">:</span> <span class="p">{</span> <span class="err">#</span> <span class="err">表示共享锁</span> 
                    <span class="nt">&#34;$numberLong&#34;</span><span class="p">:</span> <span class="s2">&#34;1000&#34;</span>
                <span class="p">}</span>
            <span class="p">}</span>
        <span class="p">},</span>
        <span class="nt">&#34;Database&#34;</span><span class="p">:</span> <span class="p">{</span> <span class="err">#</span> <span class="err">数据库锁</span>
            <span class="nt">&#34;acquireCount&#34;</span><span class="p">:</span> <span class="p">{</span>
                <span class="nt">&#34;r&#34;</span><span class="p">:</span> <span class="p">{</span>
                    <span class="nt">&#34;$numberLong&#34;</span><span class="p">:</span> <span class="s2">&#34;500&#34;</span>
                <span class="p">}</span>
            <span class="p">}</span>
        <span class="p">},</span>
        <span class="nt">&#34;Collection&#34;</span><span class="p">:</span> <span class="p">{</span> <span class="err">#</span> <span class="err">集合锁</span>
            <span class="nt">&#34;acquireCount&#34;</span><span class="p">:</span> <span class="p">{</span>
                <span class="nt">&#34;r&#34;</span><span class="p">:</span> <span class="p">{</span>
                    <span class="nt">&#34;$numberLong&#34;</span><span class="p">:</span> <span class="s2">&#34;500&#34;</span>
                <span class="p">}</span>
            <span class="p">}</span>
        <span class="p">}</span>
    <span class="p">},</span>
    <span class="nt">&#34;nreturned&#34;</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span> <span class="err">#</span> <span class="err">操作返回的文档数</span>
    <span class="nt">&#34;responseLength&#34;</span><span class="p">:</span> <span class="mi">106</span><span class="p">,</span> <span class="err">#</span> <span class="err">操作返回结果的文档长度，单位为字节</span>
    <span class="nt">&#34;protocol&#34;</span><span class="p">:</span> <span class="s2">&#34;op_query&#34;</span><span class="p">,</span> <span class="err">#</span> <span class="err">消息的协议</span>
    <span class="nt">&#34;millis&#34;</span><span class="p">:</span> <span class="mi">108</span><span class="p">,</span>  <span class="err">#</span> <span class="err">操作开始到结束耗费的时间所耗费的毫秒数。这个数字越小，说明效率越高</span>
    <span class="nt">&#34;planSummary&#34;</span><span class="p">:</span> <span class="s2">&#34;COLLSCAN&#34;</span><span class="p">,</span> <span class="err">#</span> <span class="err">摘要</span>
    <span class="nt">&#34;execStats&#34;</span><span class="p">:</span> <span class="p">{</span> <span class="err">#</span> <span class="err">操作执行过程中的详细信息</span>
        <span class="nt">&#34;stage&#34;</span><span class="p">:</span> <span class="s2">&#34;LIMIT&#34;</span><span class="p">,</span> <span class="err">#</span> <span class="err">操作形式</span> <span class="err">，COLLSCAN</span> <span class="err">用于集合扫描，IXSCAN</span> <span class="err">用于扫描索引键，FETCH</span> <span class="err">用于检索文档</span>
        <span class="nt">&#34;nReturned&#34;</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span>  <span class="err">#</span> <span class="err">返回的文档数量</span>
        <span class="nt">&#34;executionTimeMillisEstimate&#34;</span><span class="p">:</span> <span class="mi">101</span><span class="p">,</span>
        <span class="nt">&#34;works&#34;</span><span class="p">:</span> <span class="mi">63831</span><span class="p">,</span>
        <span class="nt">&#34;advanced&#34;</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
        <span class="nt">&#34;needTime&#34;</span><span class="p">:</span> <span class="mi">63830</span><span class="p">,</span>
        <span class="nt">&#34;needYield&#34;</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
        <span class="nt">&#34;saveState&#34;</span><span class="p">:</span> <span class="mi">499</span><span class="p">,</span>
        <span class="nt">&#34;restoreState&#34;</span><span class="p">:</span> <span class="mi">499</span><span class="p">,</span>
        <span class="nt">&#34;isEOF&#34;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
        <span class="nt">&#34;invalidates&#34;</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
        <span class="nt">&#34;limitAmount&#34;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
        <span class="nt">&#34;inputStage&#34;</span><span class="p">:</span> <span class="p">{</span>
            <span class="nt">&#34;stage&#34;</span><span class="p">:</span> <span class="s2">&#34;COLLSCAN&#34;</span><span class="p">,</span>
            <span class="nt">&#34;filter&#34;</span><span class="p">:</span> <span class="p">{</span>
                <span class="nt">&#34;$and&#34;</span><span class="p">:</span> <span class="p">[</span>
                    <span class="p">{</span>
                        <span class="nt">&#34;km&#34;</span><span class="p">:</span> <span class="p">{</span>
                            <span class="nt">&#34;$eq&#34;</span><span class="p">:</span> <span class="s2">&#34;km2&#34;</span>
                        <span class="p">}</span>
                    <span class="p">},</span>
                    <span class="p">{</span>
                        <span class="nt">&#34;student_id&#34;</span><span class="p">:</span> <span class="p">{</span>
                            <span class="nt">&#34;$eq&#34;</span><span class="p">:</span> <span class="s2">&#34;5c90d9162fb1c568db4b574b&#34;</span>
                        <span class="p">}</span>
                    <span class="p">}</span>
                <span class="p">]</span>
            <span class="p">},</span>
            <span class="nt">&#34;nReturned&#34;</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
            <span class="nt">&#34;executionTimeMillisEstimate&#34;</span><span class="p">:</span> <span class="mi">101</span><span class="p">,</span>
            <span class="nt">&#34;works&#34;</span><span class="p">:</span> <span class="mi">63831</span><span class="p">,</span>
            <span class="nt">&#34;advanced&#34;</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
            <span class="nt">&#34;needTime&#34;</span><span class="p">:</span> <span class="mi">63830</span><span class="p">,</span>
            <span class="nt">&#34;needYield&#34;</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
            <span class="nt">&#34;saveState&#34;</span><span class="p">:</span> <span class="mi">499</span><span class="p">,</span>
            <span class="nt">&#34;restoreState&#34;</span><span class="p">:</span> <span class="mi">499</span><span class="p">,</span>
            <span class="nt">&#34;isEOF&#34;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
            <span class="nt">&#34;invalidates&#34;</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
            <span class="nt">&#34;direction&#34;</span><span class="p">:</span> <span class="s2">&#34;forward&#34;</span><span class="p">,</span>
            <span class="nt">&#34;docsExamined&#34;</span><span class="p">:</span> <span class="mi">63829</span>
        <span class="p">}</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="1-注意">1. 注意</h3>
<p>nReturned，totalKeysExamined与totalDocsExamined，分别代表该条查询返回的条目、索引扫描条目和文档扫描条目。</p>
<p>很好理解，这些都直观的影响到executionTimeMillis，我们需要扫描的越少速度越快。</p>
<p>对于一个查询， 我们最理想的状态是</p>
<p>nReturned=totalKeysExamined &amp; totalDocsExamined=0</p>
<p>（cover index，仅仅使用到了index，无需文档扫描，这是最理想状态。）</p>
<p>或者</p>
<p>nReturned=totalKeysExamined=totalDocsExamined(需要具体情况具体分析)</p>
<p>（正常index利用，无多余index扫描与文档扫描。）</p>
<p>如果有sort的时候，为了使得sort不在内存中进行，我们可以在保证nReturned=totalDocsExamined</p>
<p>的基础上，totalKeysExamined可以大于totalDocsExamined与nReturned，因为量级较大的时候内存排序非常消耗性能。</p>
<h3 id="execstats层stage分析">execStats层，Stage分析</h3>
<h4 id="stage参数说明">stage参数说明</h4>
<table>
<thead>
<tr>
<th>type</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>IXSCAN</td>
<td>索引扫描</td>
</tr>
<tr>
<td>COLLSCAN</td>
<td>全表查询</td>
</tr>
<tr>
<td>FETCH</td>
<td>根据索引去检索指定document</td>
</tr>
<tr>
<td>SHARD_MERGE</td>
<td>将各个分片返回数据进行merge</td>
</tr>
<tr>
<td>SORT</td>
<td>在内存中进行了排序</td>
</tr>
<tr>
<td>LIMIT</td>
<td>使用了limit限制返回数</td>
</tr>
<tr>
<td>SKIP</td>
<td>使用skip进行跳过</td>
</tr>
<tr>
<td>IDHACK</td>
<td>针对_id 进行查询</td>
</tr>
<tr>
<td>SHARDING_FILTER</td>
<td>通过mongos对分片数据进行查询</td>
</tr>
<tr>
<td>COUNT</td>
<td>进行count运算</td>
</tr>
<tr>
<td>COUNTSCAN</td>
<td>Count使用了index进行count时的stage返回</td>
</tr>
<tr>
<td>COUNT_SCAN</td>
<td>Count使用了index进行count时的stage返回</td>
</tr>
<tr>
<td>SUBPLA</td>
<td>未使用到索引的$or查询的stage返回</td>
</tr>
<tr>
<td>TEXT</td>
<td>使用全文索引进行查询时候的stage返回</td>
</tr>
<tr>
<td>PROJECTION</td>
<td>限定返回字段时候stage的返回</td>
</tr>
</tbody>
</table>
<h4 id="对于普通查询我们最希望看到的组合有这些">对于普通查询，我们最希望看到的组合有这些：</h4>
<p>Fetch+IDHACK</p>
<p>Fetch+ixscan</p>
<p>Limit+（Fetch+ixscan）</p>
<p>PROJECTION+ixscan</p>
<p>SHARDING_FILTER+ixscan</p>
<p>等</p>
<h4 id="不希望看到包含如下的stage">不希望看到包含如下的stage：</h4>
<p>COLLSCAN（全表扫），SORT（使用sort但是无index），不合理的SKIP，SUBPLA（未用到index的$or）</p>
<h4 id="对于count查询希望看到的有">对于count查询，希望看到的有：</h4>
<p>COUNT_SCAN</p>
<h4 id="不希望看到的有">不希望看到的有:</h4>
<p>COUNTSCAN</p>
<h4 id="1-创建单字段索引">1. 创建单字段索引</h4>
<p><code>db.exam_note.createIndex({&quot;student_id&quot;: 1})</code></p>
<h4 id="2-复合索引">2. 复合索引</h4>
<p><code>db.exam_note.createIndex({&quot;student_id&quot;: 1, &quot;km&quot;: 1})</code></p>
<h4 id="其他类型索引">其他类型索引</h4>
<ul>
<li><a href="https://docs.mongodb.org/manual/core/index-hashed/">哈希索引（Hashed Index）</a>是指按照某个字段的hash值来建立索引，目前主要用于<a href="https://yq.aliyun.com/articles/32434?spm=5176.100238.yqhn2.22.0cUwgh">MongoDB Sharded Cluster</a>的Hash分片，hash索引只能满足字段完全匹配的查询，不能满足范围查询等。</li>
<li><a href="https://docs.mongodb.org/manual/core/2d/">地理位置索引（Geospatial Index）</a>能很好的解决O2O的应用场景，比如『查找附近的美食』、『查找某个区域内的车站』等。</li>
<li><a href="https://docs.mongodb.org/manual/core/index-text/">文本索引（Text Index）</a>能解决快速文本查找的需求，比如有一个博客文章集合，需要根据博客的内容来快速查找，则可以针对博客内容建立文本索引。</li>
</ul>
<h4 id="3-查看所有索引">3. 查看所有索引</h4>
<p><code>db.exam_note.getIndexes()</code></p>
<h4 id="4-删除所有索引">4. 删除所有索引</h4>
<p><code>db.exam_note.dropIndexes()</code></p>
<h4 id="5-删除指定索引">5. 删除指定索引</h4>
<p><code>db.exam_note.dropIndex(&quot;km_1&quot;)</code></p>
<h4 id="6-查看使用索引执行结果">6. 查看使用索引执行结果</h4>
<p><code>db.exam_note.find({&quot;student_id&quot;: &quot;606fffc6e354112bf2345a5d&quot;,&quot;km&quot;: &quot;km3&quot;}).explain(&quot;executionStats&quot;)</code></p>
<blockquote>
<p>注意：km字段的取值很有限，即拥有相同km字段的文档会有很多；而student_id字段的取值则丰富很多，拥有相同student_id字段的文档很少；显然先按student_id字段查找，再在相同student_id的文档里查找km字段更为高效。</p>
</blockquote>
<h4 id="7-案例">7. 案例</h4>
<h5 id="71-不加索引之前执行效果">7.1 不加索引之前，执行效果</h5>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-json" data-lang="json"><span class="p">{</span>
	<span class="nt">&#34;stage&#34;</span> <span class="p">:</span> <span class="s2">&#34;COLLSCAN&#34;</span><span class="p">,</span> <span class="err">#</span> <span class="err">全表扫描</span>
	<span class="nt">&#34;filter&#34;</span> <span class="p">:</span> <span class="p">{</span>
		<span class="nt">&#34;$and&#34;</span> <span class="p">:</span> <span class="p">[</span>
			<span class="p">{</span>
				<span class="nt">&#34;km&#34;</span> <span class="p">:</span> <span class="p">{</span>
					<span class="nt">&#34;$eq&#34;</span> <span class="p">:</span> <span class="s2">&#34;km3&#34;</span>
				<span class="p">}</span>
			<span class="p">},</span>
			<span class="p">{</span>
				<span class="nt">&#34;student_id&#34;</span> <span class="p">:</span> <span class="p">{</span>
					<span class="nt">&#34;$eq&#34;</span> <span class="p">:</span> <span class="s2">&#34;606fffc6e354112bf2345a5d&#34;</span>
				<span class="p">}</span>
			<span class="p">}</span>
		<span class="p">]</span>
	<span class="p">},</span>
	<span class="nt">&#34;nReturned&#34;</span> <span class="p">:</span> <span class="mi">3</span><span class="p">,</span>
	<span class="nt">&#34;executionTimeMillisEstimate&#34;</span> <span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
	<span class="nt">&#34;works&#34;</span> <span class="p">:</span> <span class="mi">784</span><span class="p">,</span>
	<span class="nt">&#34;advanced&#34;</span> <span class="p">:</span> <span class="mi">3</span><span class="p">,</span>
	<span class="nt">&#34;needTime&#34;</span> <span class="p">:</span> <span class="mi">780</span><span class="p">,</span> <span class="err">#</span> <span class="err">消耗时间</span>
	<span class="nt">&#34;needYield&#34;</span> <span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
	<span class="nt">&#34;saveState&#34;</span> <span class="p">:</span> <span class="mi">6</span><span class="p">,</span>
	<span class="nt">&#34;restoreState&#34;</span> <span class="p">:</span> <span class="mi">6</span><span class="p">,</span>
	<span class="nt">&#34;isEOF&#34;</span> <span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
	<span class="nt">&#34;invalidates&#34;</span> <span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
	<span class="nt">&#34;direction&#34;</span> <span class="p">:</span> <span class="s2">&#34;forward&#34;</span><span class="p">,</span>
	<span class="nt">&#34;docsExamined&#34;</span> <span class="p">:</span> <span class="mi">782</span> <span class="err">#</span> <span class="err">扫描全部文档数</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><h5 id="72-添加复合索引后">7.2 添加复合索引后</h5>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span><span class="lnt">42
</span><span class="lnt">43
</span><span class="lnt">44
</span><span class="lnt">45
</span><span class="lnt">46
</span><span class="lnt">47
</span><span class="lnt">48
</span><span class="lnt">49
</span><span class="lnt">50
</span><span class="lnt">51
</span><span class="lnt">52
</span><span class="lnt">53
</span><span class="lnt">54
</span><span class="lnt">55
</span><span class="lnt">56
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-json" data-lang="json"><span class="p">{</span>
	<span class="nt">&#34;stage&#34;</span> <span class="p">:</span> <span class="s2">&#34;FETCH&#34;</span><span class="p">,</span>
	<span class="nt">&#34;nReturned&#34;</span> <span class="p">:</span> <span class="mi">3</span><span class="p">,</span>
	<span class="nt">&#34;executionTimeMillisEstimate&#34;</span> <span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
	<span class="nt">&#34;works&#34;</span> <span class="p">:</span> <span class="mi">4</span><span class="p">,</span>
	<span class="nt">&#34;advanced&#34;</span> <span class="p">:</span> <span class="mi">3</span><span class="p">,</span>
	<span class="nt">&#34;needTime&#34;</span> <span class="p">:</span> <span class="mi">0</span><span class="p">,</span> <span class="err">#</span> <span class="err">消耗时间</span>
	<span class="nt">&#34;needYield&#34;</span> <span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
	<span class="nt">&#34;saveState&#34;</span> <span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
	<span class="nt">&#34;restoreState&#34;</span> <span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
	<span class="nt">&#34;isEOF&#34;</span> <span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
	<span class="nt">&#34;invalidates&#34;</span> <span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
	<span class="nt">&#34;docsExamined&#34;</span> <span class="p">:</span> <span class="mi">3</span><span class="p">,</span>
	<span class="nt">&#34;alreadyHasObj&#34;</span> <span class="p">:</span> <span class="mi">0</span>
<span class="p">}</span>
<span class="p">{</span>
	<span class="nt">&#34;stage&#34;</span> <span class="p">:</span> <span class="s2">&#34;IXSCAN&#34;</span><span class="p">,</span> <span class="err">#</span> <span class="err">使用了索引</span>
	<span class="nt">&#34;nReturned&#34;</span> <span class="p">:</span> <span class="mi">3</span><span class="p">,</span>
	<span class="nt">&#34;executionTimeMillisEstimate&#34;</span> <span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
	<span class="nt">&#34;works&#34;</span> <span class="p">:</span> <span class="mi">4</span><span class="p">,</span>
	<span class="nt">&#34;advanced&#34;</span> <span class="p">:</span> <span class="mi">3</span><span class="p">,</span>
	<span class="nt">&#34;needTime&#34;</span> <span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
	<span class="nt">&#34;needYield&#34;</span> <span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
	<span class="nt">&#34;saveState&#34;</span> <span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
	<span class="nt">&#34;restoreState&#34;</span> <span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
	<span class="nt">&#34;isEOF&#34;</span> <span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
	<span class="nt">&#34;invalidates&#34;</span> <span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
	<span class="nt">&#34;keyPattern&#34;</span> <span class="p">:</span> <span class="p">{</span>
		<span class="nt">&#34;student_id&#34;</span> <span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
		<span class="nt">&#34;km&#34;</span> <span class="p">:</span> <span class="mi">1</span>
	<span class="p">},</span>
	<span class="nt">&#34;indexName&#34;</span> <span class="p">:</span> <span class="s2">&#34;student_id_1_km_1&#34;</span><span class="p">,</span>
	<span class="nt">&#34;isMultiKey&#34;</span> <span class="p">:</span> <span class="kc">false</span><span class="p">,</span>
	<span class="nt">&#34;multiKeyPaths&#34;</span> <span class="p">:</span> <span class="p">{</span> <span class="err">#</span> <span class="err">使用了符合索引</span>
		<span class="nt">&#34;student_id&#34;</span> <span class="p">:</span> <span class="p">[</span> <span class="p">],</span>
		<span class="nt">&#34;km&#34;</span> <span class="p">:</span> <span class="p">[</span> <span class="p">]</span>
	<span class="p">},</span>
	<span class="nt">&#34;isUnique&#34;</span> <span class="p">:</span> <span class="kc">false</span><span class="p">,</span>
	<span class="nt">&#34;isSparse&#34;</span> <span class="p">:</span> <span class="kc">false</span><span class="p">,</span>
	<span class="nt">&#34;isPartial&#34;</span> <span class="p">:</span> <span class="kc">false</span><span class="p">,</span>
	<span class="nt">&#34;indexVersion&#34;</span> <span class="p">:</span> <span class="mi">2</span><span class="p">,</span>
	<span class="nt">&#34;direction&#34;</span> <span class="p">:</span> <span class="s2">&#34;forward&#34;</span><span class="p">,</span>
	<span class="nt">&#34;indexBounds&#34;</span> <span class="p">:</span> <span class="p">{</span>
		<span class="nt">&#34;student_id&#34;</span> <span class="p">:</span> <span class="p">[</span>
			<span class="s2">&#34;[\&#34;606fffc6e354112bf2345a5d\&#34;, \&#34;606fffc6e354112bf2345a5d\&#34;]&#34;</span>
		<span class="p">],</span>
		<span class="nt">&#34;km&#34;</span> <span class="p">:</span> <span class="p">[</span>
			<span class="s2">&#34;[\&#34;km3\&#34;, \&#34;km3\&#34;]&#34;</span>
		<span class="p">]</span>
	<span class="p">},</span>
	<span class="nt">&#34;keysExamined&#34;</span> <span class="p">:</span> <span class="mi">3</span><span class="p">,</span>
	<span class="nt">&#34;seeks&#34;</span> <span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
	<span class="nt">&#34;dupsTested&#34;</span> <span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
	<span class="nt">&#34;dupsDropped&#34;</span> <span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
	<span class="nt">&#34;seenInvalidated&#34;</span> <span class="p">:</span> <span class="mi">0</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div>
    </div>

    <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">古一</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">
        0001-01-01
        
    </span>
  </p>
  
  
</div>
<footer class="post-footer">
      
      <nav class="post-nav">
        <a class="prev" href="/post/js-sequence-diagrams/">
            <i class="iconfont icon-left"></i>
            <span class="prev-text nav-default">JS Sequence Diagrams</span>
            <span class="prev-text nav-mobile">上一篇</span>
          </a>
        
      </nav>
    </footer>
  </article>
        </div>
        

  

  

      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="social-links">
  <a href="/index.xml" type="application/rss+xml" class="iconfont icon-rss" title="rss"></a>
</div>

<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://gohugo.io">Hugo</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 - 
    <a class="theme-link" href="https://github.com/olOwOlo/hugo-theme-even">Even</a>
  </span>

  

  <span class="copyright-year">
    <span><a href="https://beian.miit.gov.cn/">粤ICP备20006718号-1</a></span>
    &copy; 
    2018 - 
    2022<span class="heart"><i class="iconfont icon-heart"></i></span><span>古一</span>
  </span>
</div>

    </footer>

    <div class="back-to-top" id="back-to-top">
      <i class="iconfont icon-up"></i>
    </div>
  </div>
  
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/slideout@1.0.1/dist/slideout.min.js" integrity="sha256-t+zJ/g8/KXIJMjSVQdnibt4dlaDxc9zXr/9oNPeWqdg=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.js" integrity="sha256-XVLffZaxoWfGUEbdzuLi7pwaUJv1cecsQJQqGLe7axY=" crossorigin="anonymous"></script>



<script type="text/javascript" src="/js/main.min.c99b103c33d1539acf3025e1913697534542c4a5aa5af0ccc20475ed2863603b.js"></script>








</body>
</html>
